package com.xiangwushuo.framework.activity.web.fragment

import android.app.Application
import android.net.Uri
import android.os.Bundle
import android.text.TextUtils
import com.lib.common.base.web.BaseWebViewModel
import com.lib.common.constant.INTENT_KEY_TITILE
import com.lib.common.constant.INTENT_KEY_URL
import com.lib.common.helper.api.DomainConfig
import com.lib.common.repository.datacenter.DataCenter

/**
 * Copyright (C), 2019-2020
 * FileName: WebViewModel
 * Author: Kevin
 * Date: 2020-01-15 14:22
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class WebViewModel(application: Application) : BaseWebViewModel(application) {

    /**
     * 标题
     */
    var mTitle = ""

    /**
     * 网址
     */
    var mUrl = ""

    override fun initData(bundle: Bundle?) {
        mTitle = bundle?.getString(INTENT_KEY_TITILE) ?: ""
        mUrl = bundle?.getString(INTENT_KEY_URL) ?: ""
        addParams()

//        sendEvent(WebFragment.EVENT_XXX, "extraParam")
//        sendEventToActivity(WebFragment.FRAGMENT_EVENT_XXXX)
    }

    override fun startLoadUrl() {
        loadUrl(mUrl)
    }

    /**
     * 添加公有参数
     */
    fun addParams() {
        if (!TextUtils.isEmpty(mUrl)) {
            mUrl = Uri.decode(mUrl.trim())

            if (DataCenter.isLogin()) {
                if (mUrl.contains("?")) {
                    if (!mUrl.contains("token=")) {
                        mUrl = mUrl.plus("&token=" + DataCenter.getToken())
                    }
                } else {
                    mUrl = mUrl.plus("?token=" + DataCenter.getToken())
                }
            }

            //环境
//            var type: String?
//            if (DomainConfig.getUrlEnv() == 0) {
//                type = "prod"
//            } else if (DomainConfig.getUrlEnv() == 1) {
//                type = "demo"
//            } else {
//                type = "development"
//            }
//
//            if (mUrl.contains("?")) {
//                mUrl = mUrl.plus("&be-env=".plus(type))
//            } else {
//                mUrl = mUrl.plus("?be-env=".plus(type))
//            }

//            //数媒设备id
//            mUrl =
//                mUrl.plus("&xws-sm-id=".plus(com.xiangwushuo.support.utils.Utils.getShuMeiAgentDeviceId()))
//
//            //统一请求参数。h5在App内的api请求header的风控相关字段要使用跟APP一模一样的值。
//            mUrl = mUrl.plus("&xws-app-id=".plus(WXApi.APP_ID))
//            mUrl = mUrl.plus("&xws-x-force-object=0&xws-x-platform=android")
//            mUrl = mUrl.plus("&xws-x-ver=".plus(DataCenter.getAppVersionName()))
//            mUrl = mUrl.plus("&xws-distinct-id=".plus(DataCenter.getDeviceId()))
//            mUrl = mUrl.plus(
//                "&xws-sm-device-id=".plus(com.xiangwushuo.support.utils.Utils.getShuMeiAgentDeviceId())
//            )
        }
    }
}